package server;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import bean.Cfpb;
import http.RetrofitUtil;

public class PushService extends Service {

    private Timer mTimer;
    private static Intent intent;
    private static Activity mActivity;

    public PushService() {
    }

    public static void startPushService(Activity activity){
        mActivity=activity;
        intent = new Intent(activity, PushService.class);
        activity.startService(intent);
    }

    public static void stopPushService(Activity activity){
        activity.stopService(intent);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mTimer = new Timer();
        MyTimerTask task = new MyTimerTask();
        mTimer.schedule(task,3000,5000);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mTimer!=null){
            mTimer.cancel();
        }
    }

    class MyTimerTask extends TimerTask{
        @Override
        public void run() {
            List<Cfpb> cfpbList = RetrofitUtil.postExecute(Cfpb.sql, mActivity, Cfpb.class);
            for(int i=0;i<cfpbList.size();i++){
                Cfpb cfpb = cfpbList.get(i);
                Log.e("cfpb==",cfpb.getWmdbh()+":"+cfpb.getCzmc()+":"+cfpb.getXmmc());
            }
        }
    }
}
